<template>
  <div>
    <el-col :span="8">
      <div class="l-home" style="padding-right: 10px">
        <el-card class="box-card" shadow="hover">
          <div class="user-info">
            <img src="../../../assets/images/user.png" alt="" />
            <div class="admin">
              <p class="name">Admin</p>
              <p class="identity">超级管理员</p>
            </div>
          </div>
          <div class="login-info">
            <p class="last-time">上次登录时间:<span>2023-6-15</span></p>
            <p class="last-address">上次登录地点:<span>杭州</span></p>
          </div>
        </el-card>
        <el-card class="box-card" shadow="hover" style="margin-top: 20px">
          <el-table :data="tableData" style="width: 100%">
            <el-table-column v-for="(val, key) in tableHeader" :key="key.name" :prop="key" :label="val"> </el-table-column>
          </el-table>
        </el-card>
      </div>
    </el-col>
    <el-col :span="16" style="padding-left: 10px">
      <div class="r-home">
        <el-card v-for="item in countData" :key="item.name" :body-style="{ display: 'flex', padding: '0' }">
          <i :class="`el-icon-${item.icon}`" :style="{ background: item.color }"></i>
          <div class="order">
            <p class="price">￥{{ item.value }}</p>
            <p class="kind">{{ item.name }}</p>
          </div>
        </el-card>
        <el-card style="height: 280px; width: 100%">
          <div class="line-chart" ref="lineChart" style="height: 280px"></div>
        </el-card>
        <div class="graph">
          <el-card class="bar-chart" ref="barChart" style="height: 260px"> </el-card>
          <el-card class="pie-chart" ref="pieChart" style="height: 260px"> </el-card>
        </div>
      </div>
    </el-col>
  </div>
</template>

<script>
import { getData } from '../../../api';
import * as echarts from 'echarts';
export default {
  name: 'Home',
  data() {
    return {
      tableData: [],
      orderData: [], //折线图数据
      userData: [], //柱状图数据
      videoData: [], //饼状图数据
      tableHeader: {
        name: '课程',
        todayBuy: '今日购买',
        monthBuy: '本月购买',
        totalBuy: '总购买',
      },
      countData: [
        {
          name: '今日支付订单',
          value: 1234,
          icon: 'success',
          color: '#2ec7c9',
        },
        {
          name: '今日收藏订单',
          value: 210,
          icon: 'star-on',
          color: '#ffb980',
        },
        {
          name: '今日未支付订单',
          value: 1234,
          icon: 's-goods',
          color: '#5ab1ef',
        },
        {
          name: '本月支付订单',
          value: 1234,
          icon: 'success',
          color: '#2ec7c9',
        },
        {
          name: '本月收藏订单',
          value: 210,
          icon: 'star-on',
          color: '#ffb980',
        },
        {
          name: '本月未支付订单',
          value: 1234,
          icon: 's-goods',
          color: '#5ab1ef',
        },
      ],
    };
  },
  mounted() {
    getData().then((data) => {
      this.tableData = data.data.data.tableData;
      this.orderData = data.data.data.orderData;
      this.userData = data.data.data.userData;
      this.videoData = data.data.data.videoData;
    });
  },
  updated() {
    //折线图
    const lineChart = echarts.init(this.$refs.lineChart); //实例化
    let xData = [];
    let seriesData = [];
    xData = Object.keys(this.orderData.data[0]);
    xData.forEach((key) => {
      seriesData.push({
        name: key,
        type: 'line',
        data: this.orderData.data.map((item) => item[key]), //通过遍历key去寻找value
      });
    });
    const lineOption = {
      xAxis: {
        data: xData,
      },
      legend: {
        data: xData,
      },
      yAxis: {},
      series: seriesData,
    };
    // 使用刚指定的配置项和数据显示图表。
    lineChart.setOption(lineOption);

    //柱状图
    const barChart = echarts.init(document.querySelector('.bar-chart'));
    const barOption = {
      // 图例文字颜色
      textStyle: {
        color: '#333',
      },
      grid: {
        left: '20%',
      },
      // 提示框
      tooltip: {
        trigger: 'axis',
      },
      xAxis: {
        type: 'category', // 类目轴
        data: this.userData.map((item) => item.date),
        axisLine: {
          lineStyle: {
            color: '#17b3a3',
          },
        },
        axisLabel: {
          interval: 0,
          color: '#333',
        },
      },
      yAxis: [
        {
          type: 'value',
          axisLine: {
            lineStyle: {
              color: '#17b3a3',
            },
          },
        },
      ],
      color: ['#2ec7c9', '#b6a2de'],
      series: [
        {
          name: '新增用户',
          type: 'bar',
          data: this.userData.map((item) => item.new),
        },
        {
          name: '活跃用户',
          type: 'bar',
          data: this.userData.map((item) => item.active),
        },
      ],
    };
    barChart.setOption(barOption);

    //饼状图
    const pieChart = echarts.init(document.querySelector('.pie-chart'));
    const pieOption = {
      tooltip: {
        trigger: 'item',
      },
      color: ['#0f78f4', '#dd536b', '#9462e5', '#a6a6a6', '#e1bb22', '#39c362', '#3ed1cf'],
      series: [
        {
          type:'pie',
          data:this.videoData
        }
      ],
    };
    pieChart.setOption(pieOption);
  },
};
</script>

<style lang="less" scoped>
.user-info {
  display: flex;
  justify-content: start;
  border-bottom: 1px;
  img {
    width: 150px;
    height: 150px;
    border-radius: 50%;
  }
  .admin {
    margin-left: 30px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    .name {
      font-size: 32px;
    }
    .identity {
      color: #999999;
    }
  }
}
.login-info {
  margin-top: 50px;
  .last-address,
  .last-time {
    line-height: 28px;
    font-size: 14px;
    color: #999999;
    span {
      margin-left: 50px;
      color: #666666;
    }
  }
}
.r-home {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  .graph {
    width: 100%;
    display: flex;
    justify-content: space-between;
    .el-card {
      width: 48%;
      height: 260px;
    }
  }
  .el-card {
    width: 32%;
    margin-bottom: 20px;
  }
  i {
    width: 80px;
    height: 80px;
    font-size: 30px;
    text-align: center;
    line-height: 80px;
    color: #fff;
  }
  .order {
    display: flex;
    flex-direction: column;
    justify-content: center;
    margin-left: 15px;
    .price {
      height: 30px;
      font-size: 30px;
      margin-bottom: 10px;
    }
    .kind {
      font-size: 14px;
      color: #999;
      text-align: center;
    }
  }
}
</style>
